import { Controller, Post, Body, Get } from '@nestjs/common';
import { AuthService } from './auth.service';

@Controller('auth')
export class AuthController {
  constructor(private readonly authService: AuthService) {}
  @Get()
  getHello(): string { return 'Hello World!'; }

  @Post('sms/send') // 发送验证码
  async sendCode(@Body('mobile') mobile: string) {
    console.log('mobile', mobile);
    await this.authService.sendSMSCode(mobile);
    return { code: 200, message: '验证码已发送' };
  }

  @Post('sms/login') // 验证码登录
  async smsLogin(@Body() body: { mobile: string; code: string }) {
    const data = await this.authService.loginBySMS(body.mobile, body.code);
    return { code: 200, data };
  }

  @Post('password/login') // 密码登录
  async passwordLogin(@Body() body: { mobile: string; password: string }) {
    const data = await this.authService.loginByPassword(body.mobile, body.password);
    return { code: 200, data };
  }

  @Post('password/set') // 设置密码
  async setPassword(@Body() body: { mobile: string; password: string }) {
    await this.authService.setPassword(body.mobile, body.password);
    return { code: 200, message: '密码设置成功' };
  }

  // 设置角色接口@Post('setRoles')
async setRoles(@Body() body: { mobile: string; roles: string[] }) {
  await this.authService.setRolesByMobile(body.mobile, body.roles);
  return { code: 200, message: '角色设置成功' };
}
@Post('register') // 验证码注册接口
async register(
  @Body() body: { 
    mobile: string; 
    code: string;
    name: string;
    province: string;
    gender?: string;
    school?: string;
    graduationYear?: string;
    password: string;
  }
) {
  const data = await this.authService.registerBySMS(
    body.mobile,
    body.code,
    body.name,
    body.province,
    body.gender,
    body.school,
    body.graduationYear,
    body.password
  );
  return { code: 200, data };
}
}